package org.example.service.complex;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.example.entity.OrderVO;
import org.example.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class ComplexServiceImpl implements ComplexService {

    // 复杂工作流的进程ID
    private final static String ProcessInstanceByKey = "complexProcess";

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    HistoryService historyService;

    @Override
    public String startProcess(String orderNo) {
        // 启动一个流程(businessKet业务单号)
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(ProcessInstanceByKey, orderNo);
        log.info("processInstance.getId()======={}", processInstance.getId());
        return processInstance.getId() + " success";
    }

    @Override
    public String applyPurchase(String taskId, String orderNo, Double amount) {
        Map<String, Object> map = new HashMap<>(16);
        OrderVO vo = new OrderVO();
        vo.setId(UUID.randomUUID().toString());
        vo.setOrderNo(orderNo);
        vo.setAmount(amount);
        vo.setStartTime(new Date());
        map.put("order", vo);
        log.info("map:=========={}", map);
        taskService.complete(taskId, map);
        return "申请采购单success";
    }

    @Override
    public String departmentReview(String taskId) {
        Map<String, Object> map = new HashMap<>(16);
        // 部门经理审核确认
        taskService.complete(taskId, map);
        return "部门经理审核success";
    }

    @Override
    public String projectReview(String taskId) {
        Map<String, Object> map = new HashMap<>(16);
        // 项目经理审核确认
        taskService.complete(taskId, map);
        return "项目经理审核success";
    }

    @Override
    public String financeReview(String taskId) {
        Map<String, Object> map = new HashMap<>(16);
        // 财务审核确认
        taskService.complete(taskId, map);
        return "财务审核success";
    }

    @Override
    public String managerReview(String taskId) {
        Map<String, Object> map = new HashMap<>(16);
        // 总经理审核确认
        taskService.complete(taskId, map);
        return "总经理审核success";
    }

    @Override
    public String financeManagerReview(String taskId) {
        Map<String, Object> map = new HashMap<>(16);
        // 财务总监审核确认
        taskService.complete(taskId, map);
        return "财务总监审核success";
    }
}
